<div class="bg-light lter b-b wrapper-md">
  <h1 class="m-n font-thin h3">海报管理</h1>
</div>
<div class="wrapper-md">
    <div class="row">
        <div class="col-sm-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                海报管理
                </div>
                <div  ng-controller="dataTablesCtrl">
                    <div class="row">
                        <div class="col-sm-12" >
                            <div class="panel-heading">

                            <a ui-sref="app.movie.programEdit"><button class="btn btn-default btn-sm "><i class="fa fa-plus fa-lg"></i>&nbsp; 新增</button></a>

                            <a href="javascript:void(0);" ng-click="goEdit();" ><button class="btn btn-default btn-sm "><i class="fa fa-edit fa-lg"></i>&nbsp; 编辑</button></a>

                            <a href="javascript:void(0);" ng-click="goRemove();" ><button class="btn btn-default btn-sm "><i class="fa fa-trash-o fa-lg"></i>&nbsp; 删除</button></a>

                            <!--隐藏域-->
                            <input type="hidden" id ="optIds" name="optIds">
                            </div>
                        </div>
                    </div>
                    <table id="myTable" class="table table-striped m-b-none"></table>
                </div>
            </div>
        </div>
  </div>
</div>


<!--样式-->
<style type="text/css">  
    div#rMenu {position:absolute; visibility:hidden; top:0; background-color: #555;text-align: left;padding: 2px;}  
    div#rMenu a{  
        cursor: pointer;  
        list-style: none outside none;  
    }  

.selected {
  background-color: #DFF0D8;
}
  
/* dataTables列内容居中 */
.table>tbody>tr>td{
        text-align:center;
}

/* dataTables表头居中 */
.table>thead:first-child>tr:first-child>th{
        text-align:center;
}
</style>
<!--样式-->

<script type="text/javascript">

//angularjs总控制器
app.controller('dataTablesCtrl', function($scope, $http,$state) {
//进入编辑页面
   $scope.goEdit = function () {
      var optIds = $('#optIds').val();
         if(''==optIds){
        top.layer.msg('请至少选择一项', {icon: 6});
           return false;
      }
  if(optIds.indexOf(",") != -1){
        top.layer.msg('编辑只能选择一项', {icon: 6});
        return false;
      }
        $state.go('app.movie.programPosterEdit', {infoId: optIds});
    };


//删除方法
   $scope.goRemove = function () {
      var optIds = $('#optIds').val();
         if(''==optIds){
        top.layer.msg('请至少选择一项', {icon: 6});
           return false;
        }
        top.layer.confirm('您确定删除这些信息吗', function(index){

        var postInfoUrl = API_URL+"image_info";

        var optArrs = optIds.split(',');

        for(var i = 0;i< optArrs.length;i++ ){

             var infoUrl = API_URL+"image_info?query=delflag:0,Id:"+optArrs[i];

              var formData =  MY_ASYNC_AJAX(infoUrl)[0];
             
              formData.Delflag = "1";

             $.ajax({
                 url : postInfoUrl+'/'+id,
                type : 'put',
                 data: JSON.stringify(formData),
               cache : false,
                 success : function(data) {

                  }
             });

        }
            var table = $('#myTable').DataTable();
            top.layer.msg('删除成功');
                      table.ajax.reload().draw();


        });

    
    };


});




function checkOne(){
  var optIds = '';
  $("input[name='ids']:checkbox:checked").each(function(){ 
      optIds+=$(this).val() +',';
    });
   if(''!=optIds){
        optIds = optIds.substring(0,optIds.length-1);
    }
    $('#optIds').val(optIds);

}



var dtUrl =  API_URL+"image_info?query=delflag:0";
  var dataSet;


$(document).ready(function(){

 
    $.ajax( {  
    async : false, //是否异步  
    cache : false, //是否使用缓存  
    type : 'get', //请求方式,post  
    url : dtUrl, //请求链接  
    error : function() {  
        console.log('亲，网络有点不给力呀！');  
    },  
    success : function(data) { 
      if(data.size==0){
        dataSet='';
      }else {
         dataSet=data; 
      }
    
   }  
 });


 //初始化dataTable
     var table =  $('#myTable').DataTable({

        data:dataSet,

       
         deferRender:true,
         Processing:true,

          buttons: [{
            extend: 'collection',
            text: 'Export',
            buttons: [ 'csv-flash', 'xls-flash', 'pdf-flash' ]
        }],
            //默认最后一列（最后更新时间）降序排列
            order: [[5, "desc"]],
         
            //行创建完成后回调
            rowCallback: function (row, data, index) {

               
            },
            aoColumns: [{  
            data : "Id",
            title: "<input type='checkbox' id='allChecked' name='allChecked' />",
            orderable: false , 
            render : function(data, type, full, meta) {
            return '<input type="checkbox" value="'+ data + '" name="ids" onclick="checkOne()"/>';
            }},

            {data: "ImageWidth" ,
            title: "预览"
            },
            {data: "ImageName"  ,
            title: "名称" 
            },
            {data: "AdderName"  ,
            title: "上传者" 
            },
            { data: "ProgramIsAudited" ,
                title: "审核",
                render: function (data, type, row, meta) {
                    if(row.ProgramIsAudited==0){
                        return "<i style='color:#2e8ded;' title='启用' class='fa fa-spinner fa-spin'></i>"; 
                    }else{
                    return "<i  title='停用' class='fa fa-spinner fa-spin'></i>";
                    }
                }
            },
           {data: "UpdaterId",
            title: "链接地址" 
            },
            {data: "AddTime",
                title: "上传时间",
                render: function (data, type, row, meta) {
                    return getDateTime(data);
                }
            }
        ],
        columnDefs: [{
            targets: 7,
            data: null,
            title: "操作",
            render: function (data, type, row, meta) {

                var href = '';
                  href += "&nbsp;  <a href='#/app/movie/program/edit/"+row.Id+"'>";
                  href += "<i  title='编辑' class='fa fa-edit fa-lg'></i></a>";
                  
                  href += "&nbsp;  <a href='javascript:void(0);' onclick=goDelete("
                      + row.Id
                      + ")>";
                  href += "<i  title='删除' class='fa fa-trash-o fa-lg'></i></a>";
                
              
              return href;

            }
        }],
    drawCallback: function( settings ) {

    },
    initComplete:function(data){

    //选中行
        $('#myTable tbody').on( 'click', 'tr', function () {
              
            if ( $(this).hasClass('selected') ) {
                $(this).removeClass('selected');
            }
            else {
                table.$('tr.selected').removeClass('selected');
                $(this).addClass('selected');
            }
            
        });

        //全选、反选
        $('#allChecked').on('click' , function(){
          var boxes = document.getElementsByName("ids");
            for(i=0;i<boxes.length;i++){
                        boxes[i].checked = this.checked;
            }
            var optIds ='';
            if(this.checked){
              $("input[name='ids']:checkbox:checked").each(function(){ 
              optIds+=$(this).val() +',';
               });
            }else{
                optIds='';
            }
            if(''!=optIds){
               optIds = optIds.substring(0,optIds.length-1);
            }
            $('#optIds').val(optIds);
        });
    },
    Paginate: true, 
    language:{
       url: 'vendor/jquery/datatables/Chinese.json'
    }
  });
});  





//单条删除
  function goDelete(id){
    console.log("ID:"+id);
      top.layer.confirm('您确定删除吗', function(index){
             var postInfoUrl = API_URL+"image_info";

          var infoUrl = API_URL+"image_info?query=delflag:0,Id:"+id;

          var formData =  MY_ASYNC_AJAX(infoUrl)[0];

           formData.Delflag = "1";

     $.ajax({
        url : postInfoUrl+'/'+id,
        type : 'put',
        data: JSON.stringify(formData),
        cache : false,
        success : function(data) {
          top.layer.msg('删除成功');
          /*$("#myTable").DataTable().ajax.reload().draw();*/
          window.location.reload();
        }
      });

    top.layer.close(index);
      });
  }

//修改是否展示状态
function updateDisplay(type,value,id){
  var info = '您确定';
  if(1==value){
    info+='打开';
  }else{
    info+='关闭';
  }
  
  if('pc'==type){
    info+='电脑端';
  }else if('phone'==type){
    info+='手机端';
  }else if('pad'==type){
    info+='pad端';
  }else if('tv'==type){
    info+='电视机端';
  }
  
  info+="显示吗？";
  
   //发起POST请求
  var postInfoUrl = API_URL+"image_info";

  var infoUrl = API_URL+"image_info?query=delflag:0,Id:"+id;

  var formData =  MY_ASYNC_AJAX(infoUrl)[0];
  

   if('pc' == type){
    formData.ProgramDisplayOnPc = value + "";
    }else if('phone' == type){
    formData.ProgramDisplayOnPhone = value + "";
    }else if('pad'== type){
    formData.ProgramDisplayOnPad = value + "";
    }else if('tv' == type){
    formData.ProgramDisplayOnTv = value + "";
  }


  top.layer.confirm(info, function(index){
    
    $.ajax({
      url : postInfoUrl+'/'+id,
      type : 'put',
      data: JSON.stringify(formData),
      cache : false,
      success : function(data) {
        top.layer.msg('更新成功');

         $("#myTable").DataTable().ajax.reload().draw();
      }
    });

    top.layer.close(index);
  });

}

</script>
